package com.hellpro.lolbox.home.collections;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.bean.CollectBean;
import com.hellpro.lolbox.db.CollectDao;
import com.hellpro.lolbox.home.weight.HomeZhanJIActivity;
import com.hellpro.lolbox.home.weight.ZhanJIChaXunActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;

/**
 * Package_Name: com.hellpro.lolbox.home.collections
 * Description :
 * author: HellPermanent
 * date:  2016/10/23 18:20
 * remarks：
 */
public class CollectIfro extends LolBaseActivity {
    @BindView(R.id.activity_home_colloct_button)
    Button activityHomeColloctButton;
    @BindView(R.id.collect_listview)
    PullToRefreshListView collectListview;
    @BindView(R.id.activity_home_colloct_bar)
    Toolbar activityHomeColloctBar;
    private CollectAdapter collectAdapter;
    private CollectDao collectDao;
    private SQLiteDatabase db;
    private List<CollectBean> data;

    @Override
    public int getLayoutId() {
        return R.layout.activity_collectifor;
    }

    @Override
    public void initView() {
        collectDao = new CollectDao(CollectIfro.this);
        db = collectDao.getDb();
        collectAdapter = new CollectAdapter();
        collectListview.setAdapter(collectAdapter);
        TextView textView = new TextView(this);
        textView.setText("*下拉刷新，长安删除");
        collectListview.getRefreshableView().addFooterView(textView,null,false);


        getData();
        activityHomeColloctButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CollectIfro.this, HomeZhanJIActivity.class);
                intent.putExtra("type",true);
                startActivity(intent);
                finish();
            }
        });

        //listView
        collectListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String qu = data.get(position - 1).getSn();
                String name = data.get(position - 1).getPn();
                Intent intent = new Intent(CollectIfro.this, ZhanJIChaXunActivity.class);
                intent.putExtra("qu", qu);
                intent.putExtra("name", name);
                startActivity(intent);
            }
        });

        collectListview.getRefreshableView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                CollectBean collectBean = data.get(position - 1);
                String pn = collectBean.getPn();
                db.delete("colloct_infro", "name=?", new String[]{pn});
                data.remove(position - 1);

                collectAdapter.setData(data);

                return true;
            }
        });
   activityHomeColloctBar.setNavigationOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           finish();
       }
   });
    }

    private Handler mHandler = new Handler() {


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 7) {
                data = (List<CollectBean>) msg.obj;
                collectAdapter.setData(data);
            }

        }
    };

    //从数据库里取出数据
    private void getData() {
        new Thread(

                new Runnable() {
                    @Override
                    public void run() {
                        List<CollectBean> list = new ArrayList<CollectBean>();

                        Cursor cursor = db.rawQuery("select * from colloct_infro", null);
                        while (cursor.moveToNext()) {
                            try {
                                String string = cursor.getString(3);
                                JSONObject jsonObject = new JSONObject(string);
                                Iterator<String> keys = jsonObject.keys();
                                String name = keys.next();
                                JSONObject jsonObject1 = jsonObject.getJSONObject(name);

                                String snFullName = jsonObject1.getString("snFullName");
                                String icon = jsonObject1.getString("icon");
                                String sn = jsonObject1.getString("sn");
                                String tierDesc = jsonObject1.getString("tierDesc");
                                String pn = jsonObject1.getString("pn");
                                int zdl = jsonObject1.getInt("zdl");

                                CollectBean collectBean = new CollectBean(sn, pn, icon, zdl, tierDesc, snFullName);
                                list.add(collectBean);
                                cursor.close();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                        cursor.close();
                        Message obtain = Message.obtain();
                        obtain.what = 7;
                        obtain.obj = list;
                        mHandler.sendMessage(obtain);

                    }
                }
        ).start();

    }


}
